<template>
  <div class="app-container">

    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span>临时车辆审批流程</span>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="add" style="float: right;">新增节点</el-button>
      </div>
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" stripe>
        <el-table-column fixed label="节点ID" width="80" prop="id" />
        <el-table-column label="节点名称" prop="processName" />
        <el-table-column label="上一节点ID" prop="preNodeId" />
        <el-table-column label="类型">
          <template slot-scope="scope">
            <template v-if="scope.row.type === 1">
              指定部门：{{getDeptName(scope.row.generalId)}}
            </template>
            <template v-else>所在部门</template>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)" title="修改"></el-button>
            <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row.id)" title="删除"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType === 'modify' ? '修改' : '新增'">
      <el-form ref="formRef" :model="formData" label-width="120px" label-position="right" :rules="rules">
        <el-row :gutter="20">
          <template v-if="formData.id">
            <el-col :span="12">
              <el-form-item label="ID">
                <span>{{formData.id}}</span>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="12">
            <el-form-item label="节点名称" prop="processName">
              <el-input v-model="formData.processName" placeholder="请输入节点名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上一节点" prop="preNodeId">
              <el-select v-model="formData.preNodeId" placeholder="请选择">
                <el-option
                    v-for="item in prevNodeOptions"
                    v-if="item.id != prevNodeOptions.id"
                    :key="item.id"
                    :label="item.processName"
                    :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="preNodeId">
              <el-select v-model="formData.type" placeholder="请选择">
                <el-option
                    v-for="item in typeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="formData.type === 1">
            <el-form-item label="选择指定部门" prop="generalId">
              <el-select v-model="formData.generalId" placeholder="请选择">
                <el-option
                    v-for="item in deptOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input type="textarea" :rows="2" v-model="formData.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import {
    definitionList,
    definitionInfo,
    definitionAdd,
    definitionDelete,
    definitionUpdate,
    deptList
  } from '@/api/process'

  import {
    deepClone
  } from '@/utils'

  export default {
    name: 'Process',
    data() {
      var checkEmpty = (rule, value, callback) => {
        if (!value) {
          callback(new Error("必填项不能为空！"));
        } else {
          callback();
        }
      }
      return {
        dialogType: 'create',
        dialogVisible: false,
        formData: {
          name: '来访审核',
          processDefinitionId: '1',
          preNodeId: 0,
          processName: '',
          remark: '',
          generalId: '',
          type: 0
        },
        list: [],
        prevNodeOptions: [],
        listLoading: false,
        loading: false,
        deptOptions: [],
        typeOptions: [
          {value: 0, label: '所属部门主管审批'},
          {value: 1, label: '指定部门主管审批'}
        ],
        rules: {
          proNodeId: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
          processName: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
          type: [{ required: true, validator: checkEmpty, trigger: 'blur' }],
          generalId: [{ required: true, validator: checkEmpty, trigger: 'blur' }]
        },
      }
    },
    created() {
      this.getDeptList()
    },
    mounted() {
      this.fetchData()
    },
    methods: {
      getDeptList() {
        deptList().then(response => {
          console.log('getDeptList', response)
          if (response.code === 200) {
            this.deptOptions = []
            for (let i = 0; i < response.rows.length; i++) {
              let obj = response.rows[i]
              if ( obj.parentId === 1) {
                this.deptOptions.push({
                  value: '' + obj.deptId,
                  label: obj.name
                })
              }
            }
            console.log('this.deptOptions', this.deptOptions)
          } else {
            this.$message.error('获取部门列表失败' + response.msg)
          }
        })
      },
      getDeptName(deptId) {
        for (let i = 0; i < this.deptOptions.length; i++) {
          if (this.deptOptions[i].value === deptId) {
            return this.deptOptions[i].label
          }
        }
        return '未知'
      },
      search() {
        this.fetchData()
      },
      refresh() {
        this.fetchData()
      },
      fetchData() {
        this.listLoading = true
        definitionList().then(response => {
          this.list = response.rows
          this.prevNodeOptions = []
          this.prevNodeOptions.push({id: 0, processName: '无'})
          for (let i = 0; i < this.list.length; i++) {
            this.prevNodeOptions.push({id: this.list[i].id, processName: this.list[i].processName})
          }
          this.total = response.total
          this.listLoading = false
        })
      },
      add() {
        this.dialogType = 'create'
        this.formData = {
          name: '来访审核',
          processDefinitionId: '1',
          preNodeId: 0,
          processName: '',
          remark: '',
          generalId: '',
          type: 0
        }
        this.dialogVisible = true
        this.$nextTick(()=>{
          this.$refs.formRef.clearValidate()
        })
      },
      del(id) {
        this.$confirm('确认删除该条数据吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          definitionDelete(id).then(response => {
            if (response.code === 200){
              this.fetchData()
              this.$message({
                message: '删除成功',
                type: 'success'
              })
            } else {
              this.$message.error("删除失败：" + response.msg)
            }
          })
        })
      },
      edit(row) {
        this.formData = {
          id: row.id,
          name: row.name,
          processDefinitionId: row.processDefinitionId,
          preNodeId: row.preNodeId,
          processName: row.processName,
          remark: row.remark,
          generalId: row.generalId,
          type: row.type
        }
        this.dialogType = 'modify'
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.formRef.clearValidate()
        })
      },
      submit() {
        this.$refs.formRef.validate(valid => {
          if (valid) {
            if (!this.formData.id) {
              definitionAdd(this.formData).then(response => {
                if (response.code === 200) {
                  this.$message.success('新增成功')
                  this.fetchData()
                  this.dialogVisible = false
                } else {
                  this.$message.error("新增失败：" + response.msg)
                }
              })
            } else {
              definitionUpdate(this.formData).then(response => {
                if (response.code === 200) {
                  this.$message.success('修改成功')
                  this.fetchData()
                  this.dialogVisible = false
                } else {
                  this.$message.error("修改失败：" + response.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
